<?php
namespace app\home\controller;

use app\BaseController;
use app\common\exception\ClientException;
use app\common\tool\CacheTool;
use app\model\ArticleIndex;
use app\model\Banner;
use app\model\Discuss;
use app\model\DiscussComment;
use app\model\User;
use think\App;
use think\exception\ValidateException;

class Home extends BaseController
{
    protected $current_user_id;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->current_user_id = (new CacheTool())->getCache(config('secure.current_user_key'));
    }

    /**
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\DataNotFoundException
     */
    public function getBanner(): \think\response\Json
    {
        $banner = Banner::with('image')->select();

        return json($banner);
    }

    /**
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\DataNotFoundException
     */
    public function getArticleIndex(): \think\response\Json
    {
        $article_index = ArticleIndex::select();

        return json($article_index);
    }

    /**
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\DataNotFoundException
     */
    public function getArticleByIndex($id): \think\response\Json
    {
        $article = ArticleIndex::with(['article', 'article.user'])->find($id);

        return json($article);
    }

    public function getDiscuss()
    {
        $page = $this->request->param('page', 1);
        $discuss = Discuss::with(['user', 'user.image'])
            ->order('create_time desc')
            ->page($page, 20)
            ->select();
        $total = Discuss::count();
        $result = [
            'data' => $discuss,
            'total' => $total
        ];
        return json($result);
    }

    public function getDiscussDetail($id)
    {
        $discuss_detail = Discuss::with(['user', 'user.image', 'discuss', 'discuss.user.image'])->find($id);
        $discuss_detail->pv += 1;
        $discuss_detail->save();
        return json($discuss_detail);
    }

    public function reply()
    {
        $content = $this->request->param(['content', 'article_id']);

        $this->discussCommentAdd($content['article_id']);

        try {
            $this->validate($content, 'HomeValidate.comment');
        } catch (ValidateException $e) {
            throw new ClientException(['msg' => $e->getMessage()]);
        }

        DiscussComment::create([
            'uid' => $this->current_user_id,
            'content' => $content['content'],
            'discuss_id' => $content['article_id']
        ]);
    }

    public function discussCommentAdd($id)
    {
        $discuss = Discuss::find($id);
        $discuss->callback += 1;
        $discuss->save();
    }

    public function getCurrentUser()
    {
        $user = User::with('image')->field(['id', 'img_id', 'name'])->find($this->current_user_id);
        return json($user);
    }
}
